package com.ymt.fmd.platform.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.ymt.fmd.appboot.prop.OssFormProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

/**
 * This util demonstrates how to get started with basic requests to Aliyun OSS 
 * using the OSS SDK for Java.
 */
@Service
@EnableConfigurationProperties(value = {OssFormProperties.class})
public class OssFormService {

    @Autowired
    private OssFormProperties ossFormProperties;

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
    public void upload(String key, String filePath, ObjectMetadata meta) throws Exception {
    	upload(key, new File(filePath), meta);
    }
    
    public void upload(String key, File file, ObjectMetadata meta) throws Exception {
    	upload(key, new FileInputStream(file), meta);
    }
    
    /**
     * upload a file to oss
     * @param key
     * @param is
     * @param meta
     * @throws Exception
     */
    public void upload(String key, InputStream is, ObjectMetadata meta) throws Exception {
		String endpoint = ossFormProperties.getSchema() + ossFormProperties.getEndpoint();
		String accessKeyId = ossFormProperties.getAccessKeyId();
		String accessKeySecret = ossFormProperties.getAccessKeySecret();
		String bucketName = ossFormProperties.getBucketName();
    	OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    	try {
    		PutObjectResult rs = ossClient.putObject(new PutObjectRequest(bucketName, key, is, meta));
    		if (logger.isDebugEnabled()) {
				logger.debug("PutObjectResult response="+rs);
			}
    	} catch (OSSException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		} finally {
    		ossClient.shutdown();
    	}
    }
    
    /**
     * delete a file from oss
     * @param key
     * @throws Exception
     */
    public void delete(String key) throws Exception {
		String endpoint = ossFormProperties.getSchema() + ossFormProperties.getEndpoint();
		String accessKeyId = ossFormProperties.getAccessKeyId();
		String accessKeySecret = ossFormProperties.getAccessKeySecret();
		String bucketName = ossFormProperties.getBucketName();
    	OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    	try {
    		ossClient.deleteObject(bucketName, key);
    	} finally {
    		ossClient.shutdown();
    	}
    }
    
    /**
     * set file acl on oss
     * @param key
     * @param acl
     * @throws Exception
     */
    public void setAcl(String key, CannedAccessControlList acl) throws Exception {
		String endpoint = ossFormProperties.getSchema() + ossFormProperties.getEndpoint();
		String accessKeyId = ossFormProperties.getAccessKeyId();
		String accessKeySecret = ossFormProperties.getAccessKeySecret();
		String bucketName = ossFormProperties.getBucketName();
    	OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    	try {
    		boolean exists = ossClient.doesObjectExist(bucketName, key);
            System.out.println("Does object " + bucketName + " exist? " + exists + "\n");
            
            ossClient.setObjectAcl(bucketName, key, acl);
    	} finally {
    		ossClient.shutdown();
    	}
    }
    
    /**
     * check file exists
     * @param key
     * @return
     * @throws Exception
     */
    public boolean exists(String key) throws Exception {
		String endpoint = ossFormProperties.getSchema() + ossFormProperties.getEndpoint();
		String accessKeyId = ossFormProperties.getAccessKeyId();
		String accessKeySecret = ossFormProperties.getAccessKeySecret();
		String bucketName = ossFormProperties.getBucketName();
    	OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    	try {
    		return ossClient.doesObjectExist(bucketName, key);
    	} finally {
    		ossClient.shutdown();
    	}
    }
    
}
